from marshmallow import fields, validates_schema, ValidationError

from gis.common.django_ext.forms import CNDatetimeField, PaginationSchema


class ListTopicSchema(PaginationSchema):
    id = fields.Integer(allow_none=True)
    agency_id = fields.Integer(allow_none=True)
    parent_name = fields.String(allow_none=True)
    parent_id = fields.Integer(allow_none=True)
    agency_name = fields.String(allow_none=True)
    is_replied = fields.Boolean(allow_none=True)
    play_mch_id = fields.Integer(allow_none=True)
    play_id = fields.Integer(allow_none=True)
    start_updated_at = CNDatetimeField(allow_none=True)
    end_updated_at = CNDatetimeField(allow_none=True)

    @validates_schema
    def validate_play_id(self, data):
        if data.get('play_id') and not data.get('play_mch_id'):
            raise ValidationError('填写用户id时，请先选择用户所属商户平台')


class TopicMessagesSchema(PaginationSchema):
    last_msg_id = fields.Integer(allow_none=True)
    page_size = fields.Integer(missing=30)


class MessagesSchema(PaginationSchema):
    last_msg_id = fields.Integer(allow_none=True)
    page_size = fields.Integer(missing=30)
    agency_id = fields.Integer()
    play_mch_id = fields.Integer()
    player_id = fields.Integer()
